package com.mt.api.task;

import cn.hutool.core.thread.ThreadUtil;
import com.mt.api.modules.domain.AppCargo;
import com.mt.api.modules.domain.AppCustomer;
import com.mt.api.modules.domain.dto.OrderCargoCountDto;
import com.mt.api.modules.domain.dto.OrderCustomerCountDto;
import com.mt.api.modules.manager.ICargoManager;
import com.mt.api.modules.manager.ICustomerManager;
import com.mt.api.modules.manager.IOrderManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @Author: plover
 * @Date: 2022/01/02 6:14 下午
 * @Description:
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ComputeWeightTask {

    private final IOrderManager orderManager;
    private final ICustomerManager customerManager;
    private final ICargoManager cargoManager;

    @Scheduled(cron = "0 0 2 * * ?")
    public void run() {
        log.info("ComputeWeightTask开始执行");
        try {
            int lastDayNum = 7;
            ThreadUtil.execAsync(() -> {
                //更新客户权重
                List<OrderCustomerCountDto> orderCustomerCountDtoList = orderManager.getOrderCustomerCount(lastDayNum);
                log.info("【ComputeWeightTask】更新客户权重开始，orderCustomerCountDtoList: {}", orderCustomerCountDtoList.size());
                for (OrderCustomerCountDto orderCustomerCountDto : orderCustomerCountDtoList) {
                    Integer customerId = orderCustomerCountDto.getCustomerId();
                    Integer cou = orderCustomerCountDto.getCou();
                    AppCustomer appCustomer = new AppCustomer();
                    appCustomer.setId(customerId);
                    appCustomer.setWeight(cou);
                    customerManager.updateById(appCustomer);
                }
                log.info("【ComputeWeightTask】更新客户权重结束");
            });
            ThreadUtil.execAsync(() -> {
                //更新货号权重
                List<OrderCargoCountDto> orderCargoCountDtoList = orderManager.getOrderCargoCount(lastDayNum);
                log.info("【ComputeWeightTask】更新款号权重开始，orderCargoCountDtoList: {}", orderCargoCountDtoList.size());
                for (OrderCargoCountDto orderCargoCountDto : orderCargoCountDtoList) {
                    Integer brandId = orderCargoCountDto.getBrandId();
                    String cargoNo = orderCargoCountDto.getCargoNo();
                    Integer cou = orderCargoCountDto.getCou();

                    AppCargo cargoByCargoNo = cargoManager.getCargoByCargoNo(cargoNo);
                    if (Objects.isNull(cargoByCargoNo)) {
                        log.info("【ComputeWeightTask】更新款号权重异常，款号不存在，brandId: {}，cargoNo: {}", brandId, cargoNo);
                    }
                    if (!Objects.equals(cargoByCargoNo.getWeight(), cou)) {
                        AppCargo upAppCargo = new AppCargo();
                        upAppCargo.setId(cargoByCargoNo.getId());
                        upAppCargo.setWeight(cou);
                        cargoManager.updateById(upAppCargo);
                    }
                }
                log.info("【ComputeWeightTask】更新款号权重结束");
            });
        } catch (Exception e) {
            log.error("ComputeWeightTask执行失败：", e);
        }

    }
}
